/*
 * This file is part of qmsystem-qml-example package
 *
 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,     
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS  
 * IN THE SOFTWARE. 
 */

#include "qmsystemexample.h"
#include "qmdevicemode.h"
#include <qmsysteminformation.h>

QmSystemExample::QmSystemExample(QObject *parent) :
    QObject(parent)
{
    m_battery = new QmBattery();
    m_deviceMode = new QmDeviceMode();
    m_time = new QmTime();

    //! Start listening for MeeGo::QmTime::timeOrSettingsChanged signal
    connect(m_time, SIGNAL(timeOrSettingsChanged(MeeGo::QmTime::WhatChanged)), this, SLOT(settingsChanged(MeeGo::QmTime::WhatChanged)));

    //! Emit signal when device operation mode changed
    connect(m_deviceMode, SIGNAL(deviceModeChanged(MeeGo::QmDeviceMode::DeviceMode)), this, SIGNAL(deviceModeChanged()));

    //! Start listening for signals from m_battery and emit NOTIFY signals for properties when needed
    connect(m_battery, SIGNAL(batteryRemainingCapacityChanged(int, int)), this, SIGNAL(remainingTalkTimeChanged()));
    connect(m_battery, SIGNAL(batteryRemainingCapacityChanged(int, int)), this, SIGNAL(remainingActiveTimeChanged()));
    connect(m_battery, SIGNAL(batteryRemainingCapacityChanged(int, int)), this, SIGNAL(remainingIdleTimeChanged()));
}

QmSystemExample::~QmSystemExample()
{
    if (m_battery) {
        m_battery->disconnect();
        delete m_battery;
    }

    if (m_deviceMode) delete m_deviceMode;
    if (m_time) delete m_time;
}

int QmSystemExample::remainingTalkTime() const
{
    //! QmDeviceMode class provides information and actions on device operation mode and power save mode
    QmDeviceMode deviceMode;

    //! Get current power save mode of the device and check remaining talk time based on it
    if (deviceMode.getPSMState() == QmDeviceMode::PSMStateOn)
        return m_battery->getRemainingTalkTime(QmBattery::PowersaveMode);
    else
        return m_battery->getRemainingTalkTime(QmBattery::NormalMode);
}

int QmSystemExample::remainingActiveTime() const
{
    QmDeviceMode deviceMode;

    //! Get current power save mode of the device and check remaining active use time based on it
    if (deviceMode.getPSMState() == QmDeviceMode::PSMStateOn)
        return m_battery->getRemainingActiveTime(QmBattery::PowersaveMode);
    else
        return m_battery->getRemainingActiveTime(QmBattery::NormalMode);
}

int QmSystemExample::remainingIdleTime() const
{
    QmDeviceMode deviceMode;

    //! Get current power save mode of the device and check remaining idle time based on it
    if (deviceMode.getPSMState() == QmDeviceMode::PSMStateOn)
        return m_battery->getRemainingIdleTime(QmBattery::PowersaveMode);
    else
        return m_battery->getRemainingIdleTime(QmBattery::NormalMode);
}

QString QmSystemExample::deviceMode() const
{
    QString str;

    switch ( m_deviceMode->getMode() ) {
    case QmDeviceMode::Normal:
        str = "Normal";
        break;
    case QmDeviceMode::Flight:
        str = "Flight mode";
        break;
    default:
        str = "Unknown";
        break;
    }

    return str;
}

void QmSystemExample::settingsChanged(MeeGo::QmTime::WhatChanged what)
{
    Q_UNUSED(what);

    //! Emmiting signals
    emit autoSystemTimeChanged();
    emit autoTimeZoneChanged();
    emit timezoneChanged();
    emit timezoneAutoChanged();
}

QString QmSystemExample::autoSystemTime() const
{
    QString str;

    switch ( m_time->autoSystemTime()) {
    case QmTime::AutoSystemTimeOff:
        str = "Set manually by a user";
        break;
    case QmTime::AutoSystemTimeOn:
        str = "Set automatically";
        break;
    default:
        str = "Unknown";
        break;
    }

    return str;
}

QString QmSystemExample::autoTimeZone() const
{
    QString str;

    switch ( m_time->autoTimeZone()) {
    case QmTime::AutoTimeZoneOff:
        str = "Set manually by a user";
        break;
    case QmTime::AutoTimeZoneOn:
        str = "Guessed automatically";
        break;
    default:
        str = "Unknown";
        break;
    }

    return str;
}

QString QmSystemExample::timezone() const
{
    //! String variable to store the result
    QString str;

    if (m_time->getTimezone(str))
        return str;
    else
        return ("Not Available");
}

QString QmSystemExample::timezoneAuto() const
{
    //! String variable to store the result when m_time->getAutoTimezone(str) executed
    QString str;

    if (m_time->getAutoTimezone(str))
        return str;
    else
        return ("Not Available");
}

/*!
 * Function to check if device supports time updates from cellular network operator and
 * convert result to string that can be displayed to user.
 */
QString QmSystemExample::timeFromOperator() const
{
    //! Boolean variable to store the result
    bool bln;

    if (m_time->isOperatorTimeAccessible(bln)) {
        if (bln)
            return ("Supported");
        else
            return ("Not supported");

    } else
        return ("Not Available");
}

/*!
 * Function to get default time zone defined during device manufacture.
 */
QString QmSystemExample::defaultTimeZone() const
{
    //! String variable to store the result
    QString str;

    if (m_time->deviceDefaultTimezone(str))
        return str;
    else
        return ("Not Available");
}

QString QmSystemExample::releaseVersion() const
{
    QmSystemInformation::OperatingSystemVersion ver = QmSystemInformation::operatingSystemVersion();
    QString str;
    if (ver == QmSystemInformation::OSVersion_Unknown) {
        str = "Unknown OS version";
    } else if (ver == QmSystemInformation::OSVersion_Harmattan_1_0) {
        str = "Harmattan PR 1.0";
    } else if (ver == QmSystemInformation::OSVersion_Harmattan_1_1) {
        str = "Harmattan PR 1.1";
    } else if (ver == QmSystemInformation::OSVersion_Harmattan_1_1_1) {
        str = "Harmattan PR 1.1.1";
    } else if (ver == QmSystemInformation::OSVersion_Harmattan_1_2) {
        str = "Harmattan PR 1.2";
    }

    return str;
}

